/**
  ******************************************************************************
  * @file           : TouchPad.h
  * @author         : admin
  * @brief          : None
  * @attention      : None
  * @date           : 2024/10/11
  ******************************************************************************
  */

#ifndef TOUCHPAD_H
#define TOUCHPAD_H
#include <Arduino.h>
#define ARDUINO_RUNNING_CORE 1
typedef void (*CBFunc)(void);
class TouchPad
{
public:
    uint16_t threshold = 35;
    TouchPad(uint8_t _io);
    bool isTouched(void);
    void setTouchedCallback(CBFunc _cb);
    void setUnTouchedCallback(CBFunc _cb);
    uint32_t read(void);
private:
    CBFunc touchedCb;
    CBFunc untouchedCb;
    uint8_t io;
    int valuePrevPrev,valuePrev,value;
    static void taskLoop(void *param){
        bool prevState = false;
        TouchPad *self = (TouchPad *)param;
        while(1){
            bool isTouched = self->isTouched();
            if(prevState != isTouched){
                if(isTouched && self->touchedCb){
                    self->touchedCb();
                }
                if((!isTouched) && self->untouchedCb){
                    self->untouchedCb();
                }
                prevState = isTouched;
            }
            delay(10);
        }
    }
};
extern TouchPad touchPadP,touchPadY,touchPadT,touchPadH,touchPadO,touchPadN;
#endif //XYXKC_ESP32_E_TOUCHPAD_H
